using System;
using System.Windows;

namespace Columbus
{
    /// <summary>
    /// Dispatcher is used to execute actions on main thread
    /// </summary>
    public static class Dispatcher
    {
        static Dispatcher()
        {
            ResetToDefaults();
        }

        /// <summary>
        /// Dispatches action back to main thread
        /// </summary>
        public static Action<Action> Dispatch;

        /// <summary>
        /// Resets class actions and functions back to their default implementations
        /// </summary>
        public static void ResetToDefaults()
        {
            ResetDispatch();
        }

        private static void ResetDispatch()
        {
            Dispatch = action =>
            {
                if (Deployment.Current.Dispatcher.CheckAccess())
                    action();
                else
                    Deployment.Current.Dispatcher.BeginInvoke(action);
            };
        }
    }
}